<?php

class Admin_CommunityController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    /**
     * 论坛首页
     * 
     * 显示所有分区和版块
     */
    public function homeAction()
    {
        $modelCategory = new Ms_Model_ForumCategory();
        $modelForum = new Ms_Model_Forum();
        $order = "positon ASC";
        $categories = $modelCategory->getCategories(null, $order);
        if (count($categories) > 0){
            $categories = $categories->toArray();
            $i = 0;
            foreach ($categories as $category){
                $where = array('cat_id'=>$category['id']);
                $forums = $modelForum->getForums($where);
                if (count($forums) > 0){
                    $forums = $forums->toArray();
                    $categories[$i]['forums'] = $forums;
                    $i++;
                }
            }
            $this->view->categories = $categories;
        }
        else{
            echo "目前还没有论坛分区";
        }        
    }

    /**
     * 创建论坛分区
     * 
     */
    public function createCategoryAction()
    {
        $formCategory = new Admin_Form_Forum();
        $formCategory->removeElement('cat_id');
        $formCategory->removeElement('name');
        $formCategory->removeElement('description');
        if ($this->getRequest()->isPost()){
            if ($formCategory->isValid($_POST)){
                $modelCategory = new Admin_Model_ForumCategory();
                $catname = $formCategory->getValue('catname');
                $newCategory = $modelCategory->createCategory($catname);
                if ($newCategory){
                    $this->_redirect('/admin/community/home');
                }
            }
        }
        $this->view->formCategory = $formCategory;
    }

    /**
     * 更新论坛分区
     * 
     */
    public function updateCategoryAction()
    {
        $id = $this->_request->getParam('id');
        $formCategory = new Admin_Form_Forum();
        $formCategory->removeElement('cat_id');
        $formCategory->removeElement('name');
        $formCategory->removeElement('description');
        $modelCategory = new Admin_Model_ForumCategory();
        if ($this->getRequest()->isPost()){
            if ($formCategory->isValid($_POST)){
                $catname = $formCategory->getValue('catname');
                $newCategory = $modelCategory->updateCategory($id, $catname);
                if ($newCategory != false){
                    $this->_redirect('/admin/community/home');
                }
            }
        }
        $currentCategory = $modelCategory->find($id)->current()->toArray();
        $formCategory->populate($currentCategory);
        $this->view->formCategory = $formCategory;
    }

    /**
     * 删除论坛分区
     * 
     */
    public function deleteCategoryAction()
    {
        $id = $this->_request->getParam('id');
        $modelCategory = new Admin_Model_ForumCategory();
        $modelCategory->deleteCategory($id);
        if ($modelCategory == true){
            $this->_redirect('/admin/community/home');
        }
    }

    /**
     * 分区列表
     * 
     * 转换为JSON格式，使用autoCompleteDojo助手提供响应
     */
    public function listCategoryAction()
    {
        $modelCategory = new Ms_Model_ForumCategory();
        $categories = $modelCategory->getCategories();
        $data = new Zend_Dojo_Data('id', $categories);
        $this->_helper->autoCompleteDojo($data);
    }

    /**
     * 移动分区
     * 
     * 通过改变分区的position来移动分区位置
     */
    public function moveCategoryAction()
    {
        $id = $this->_request->getParam('id');
        $direction = $this->_request->getParam('direction');

        $modelCategory = new Admin_Model_ForumCategory();
        $category = $modelCategory->find($id)->current();
        if($direction == 'up'){
            $modelCategory->moveUp($id);
        }
        if($direction == 'down'){
            $modelCategory->moveDown($id);
        }
        $this->_redirect('/admin/community/home');
    }
    
    /**
     * 创建论坛版块
     * 
     */
    public function createForumAction()
    {
        $formForum = new Admin_Form_Forum();
        $formForum->removeElement('catname');
        if ($this->getRequest()->isPost()){
            if ($formForum->isValid($_POST)){
                $modelForum = new Admin_Model_Forum();
                $cat_id = $formForum->getValue('cat_id');
                $name = $formForum->getValue('name');
                $description = $formForum->getValue('description');
                $newForum = $modelForum->createForum($cat_id, $name, $description);
                if ($newForum){
                    $this->_redirect('/admin/community/home');
                }
            }
        }
        $this->view->formForum = $formForum;
    }
    
    /**
     * 更新论坛版块
     * 
     */
    public function updateForumAction()
    {
        $id = $this->_request->getParam('id');
        $formForum = new Admin_Form_Forum();
        $formForum->removeElement('catname');
        $modelForum = new Admin_Model_Forum();
        if ($this->getRequest()->isPost()){
            if ($formForum->isValid($_POST)){
                $cat_id = $formForum->getValue('cat_id');
                $name = $formForum->getValue('name');
                $description = $formForum->getValue('description');
                $newForum = $modelForum->updateForum($id, $cat_id, $name, $description);
                if ($newForum != false){
                    $this->_redirect('/admin/community/home');
                }
            }
        }
        $currentForum = $modelForum->find($id)->current()->toArray();
        $formForum->populate($currentForum); 
        $this->view->formForum = $formForum;
    }
    
    /**
     * 删除论坛版块
     * 
     */
    public function deleteForumAction()
    {
        $id = $this->_request->getParam('id');
        $modelForum = new Admin_Model_Forum();
        $modelForum->deleteForum($id);
        if ($modelForum == true){
            $this->_redirect('/admin/community/home');
        }
    }

}
